home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Science⁄Math / Scientist's Helper src / s.helper.3 / reggraph.c < prev    next >
C/C++ Source or Header  |  1986-02-05  |  6KB  |  319 lines

  1. #include "all.h"
  2. #include "regtabext.h"
  3.  
  4. NextNotNan(startRow, x, y, foundRow)
  5. int startRow,  x,  y,  *foundRow;
  6. /*searches through two columns of table, and returns first row after*/
  7. /*and including startRow that has no Nans in the columns*/
  8. {
  9.     int i, bothNaN;
  10.     float fx, fy;
  11.  
  12.     *foundRow = 0;
  13.     bothNaN=TRUE;
  14.     for (i=startRow;  ((i<=table.header.rows) && bothNaN); i++ ) {
  15.         GetTable( i, x, &fx );
  16.         GetTable( i, y, &fy );
  17.         if( (!NaN(&fx)) && (!NaN(&fy))  ) {
  18.             *foundRow = i;
  19.             bothNaN = FALSE;
  20.             } /* end if */
  21.         } /*end for*/
  22.     return(!bothNaN);
  23. }
  24.  
  25. GraphScale( x, y, ix, iy ) /*to pixel coordinates*/
  26. float x, y;
  27. int *ix, *iy;
  28. {
  29.     float xs, ys;
  30.     xs  = ((float)(grXMax-grXMin))*(x-graph.xMin)/(graph.xMax-graph.xMin);
  31.     ys  = ((float)(grYMin-grYMax))*(y-graph.yMin)/(graph.yMax-graph.yMin);
  32.     xs  = xs + (float)grXMin;
  33.     ys  = (float)grYMin-ys;
  34.     
  35.     if( (xs>=(-32760.0)) && (xs<=32760.0) ) {
  36.         *ix=(int)xs;
  37.         }
  38.     else if( xs<(-32760.0) ) {
  39.         *ix = -32760;
  40.         }
  41.     else {
  42.         *ix = 32760;
  43.         }
  44.  
  45.     if( (ys>=(-32760.0)) && (ys<32760.0) ) {
  46.         *iy=(int)ys;
  47.         }
  48.     else if( ys<(-32760) ) {
  49.         *iy  =  -32760;
  50.         }
  51.     else {
  52.         *iy = 32760;
  53.         }
  54. }
  55.  
  56. InvGraphScale( ix, iy, x, y)   /*pixels to graph coordinates*/
  57. int ix, iy;
  58. float *x, *y;
  59. {
  60.     ix = ix-grXMin;
  61.     iy = grYMin-iy;
  62.     *x  =( ( (float)(ix) / ((float)(grXMax-grXMin)) ) * (graph.xMax-graph.xMin) ) + graph.xMin;
  63.     *y  =( ( (float)(iy) / ((float)(grYMin-grYMax)) ) * (graph.yMax-graph.yMin) ) + graph.yMin;
  64. }
  65.  
  66. PlotCol()
  67. {
  68.     int xcol, ycol, firstRow, nextRow, i, j, oldWindow, width;
  69.     int startRow, endRow, dx, dy, continuous, pat, refresh;
  70.     float x1, y1;
  71.     char symbol, s[cmdWordLen];
  72.     GrafPtr oldPort;
  73.     
  74.     SToI( command.cmdWord[1], &xcol );
  75.     SToI( command.cmdWord[2], &ycol );
  76.     if (GoodCol(xcol)!=0) {
  77.         ErrMsg("bad x col");
  78.         }
  79.     if (GoodCol(ycol)!=0) {
  80.         ErrMsg("bad y col");
  81.         }
  82.         
  83.     if( table.header.rows >= 1000 ) {
  84.         refresh = 100;
  85.         }
  86.     else {
  87.         refresh = 10;
  88.         }
  89.         
  90.     strcpy( s, command.cmdWord[3] );
  91.     pat=0;
  92.     width=1;
  93.     if( (strlen(s)==0) || (strcmp(s,"solid")==0) ){
  94.         continuous =TRUE;
  95.         pat=0; /*black*/
  96.         width=1;
  97.         }
  98.     else if (strcmp(s,"dashed")==0) {
  99.         continuous =TRUE;
  100.         width=2;
  101.         pat=1; /*gray*/
  102.         }
  103.     else if (strcmp(s,"dotted")==0) {
  104.         continuous = TRUE;
  105.         pat=2; /*ltGray*/;
  106.         width=2;
  107.         }
  108.     else if (strcmp(s,"bold")==0) {
  109.         continuous = TRUE;
  110.         pat=0; /*black*/
  111.         width=2;
  112.         }
  113.     else if (strcmp(s,"dots")==0) {
  114.         continuous =FALSE;
  115.         dx=2;
  116.         dy =1;
  117.         symbol='.';
  118.         }
  119.     else if (strcmp(s,"crosses")==0) {
  120.         continuous=FALSE;
  121.         symbol = '+';
  122.         dx=2;
  123.         dy=5;
  124.         }
  125.     else if (strcmp(s,"circles")==0) {
  126.         continuous = FALSE;
  127.         symbol = 'o';
  128.         dx = 3;
  129.         dy = 3;
  130.         }
  131.     else if (strcmp(s,"stars")==0) {
  132.         continuous = FALSE;
  133.         symbol = '*';
  134.         dx = 2;
  135.         dy = 5;
  136.         }
  137.     else if (strcmp(s,"x")==0) {
  138.         continuous = FALSE;
  139.         symbol = 'x';
  140.         dx =  2;
  141.         dy =  3;
  142.         }
  143.     else {
  144.         ErrMsg("bad plotting symbol");
  145.         }
  146.         
  147.     GetPort( &oldPort );
  148.     oldWindow=currentWindow;
  149.     
  150.     SetPort(grPortPtr);
  151.     TextFont(monaco);
  152.     TextSize(9);
  153.     
  154.     if( pat==1 ) {
  155.         PenPat( gray );
  156.         }
  157.     else if( pat==2 ) {
  158.         PenPat( ltGray );
  159.         }
  160.     else {
  161.         PenPat( black );
  162.         }
  163.     PenSize( width, width );
  164.  
  165.     /*determine limits: dont plot interpolated data outside box*/
  166.     if( (table.header.interpolated) && (xcol==1) ) {
  167.         x1=1.0 + (graph.xMin-table.header.start)/table.header.samp;
  168.         if ((x1-1.0)<1.0) {
  169.             startRow =1;
  170.             }
  171.         else if ((x1-1.0)>((float)table.header.rows)) {
  172.             startRow = table.header.rows;
  173.             }
  174.         else {
  175.             startRow =  (int)(x1-1.0);
  176.             }
  177.         x1 = 1.0 + (graph.xMax-table.header.start)/table.header.samp;
  178.         if( (x1+1.0)<1.0 ) {
  179.             endRow=1;
  180.             }
  181.         else if( (x1+1.0)>((float)table.header.rows) ) {
  182.             endRow=table.header.rows;
  183.             }
  184.         else {
  185.             endRow = (int)(x1+1.0);
  186.             }
  187.         }
  188.     else {
  189.         startRow =1;
  190.         endRow = table.header.rows;
  191.         }
  192.  
  193.     if( NextNotNan(startRow,xcol,ycol, &firstRow) ) {
  194.         GetTable( firstRow, xcol, &x1 );
  195.         GetTable( firstRow, ycol, &y1 );
  196.         GraphScale( x1, y1, &i, &j );
  197.         if (continuous) {
  198.             MoveTo( i, j );
  199.             }
  200.         else {
  201.             MoveTo( i-dx, j+dy );
  202.             DrawText(&symbol, 0, 1);
  203.             }
  204.         firstRow++;
  205.         while( (firstRow<=endRow) && NextNotNan(firstRow,xcol,ycol,&nextRow)  ) {
  206.             GetTable( nextRow, xcol, &x1 );
  207.             GetTable( nextRow, ycol, &y1 );
  208.             GraphScale( x1, y1, &i, &j );
  209.             if (continuous) {
  210.                 LineTo( i, j );
  211.                 }
  212.             else {
  213.                 MoveTo( i-dx, j+dy );
  214.                 DrawText(&symbol,0,1);
  215.                 }
  216.             firstRow=nextRow + 1;
  217.             if( (nextRow%refresh)==0) {
  218.                 CheckAbortMenu();
  219.                 currentWindow=grWindow;
  220.                 whichWindow=theWindow[grWindow];
  221.                 SetPort( whichWindow );
  222.                 DrawWindow();
  223.                 SetPort( grPortPtr );
  224.                 }
  225.             } /*end while*/
  226.         } /*end if*/
  227.  
  228.     SetPort( theWindow[grWindow] );
  229.     InvalRect( &(theWindow[grWindow]->portRect) );
  230.     
  231.     SetPort( oldPort );
  232.     currentWindow=oldWindow;
  233.     whichWindow=theWindow[currentWindow];
  234. }
  235.  
  236. ClearGraph()
  237. {
  238.     int oldWindow;
  239.     GrafPtr oldPort;
  240.     
  241.     GetPort( &oldPort );
  242.     oldWindow=currentWindow;
  243.  
  244.     SetPort( grPortPtr );
  245.     EraseRect( &(grPort.portRect) );
  246.     FrameRect(&(grPort.portRect) );
  247.     
  248.     SetPort( theWindow[grWindow] );
  249.     InvalRect( &(theWindow[grWindow]->portRect) );
  250.     
  251.     SetPort( oldPort );
  252.     currentWindow=oldWindow;
  253.     whichWindow=theWindow[currentWindow];
  254. }
  255.  
  256. PlotAxes()
  257. {
  258.     int i, ii, j, k, width, slen;
  259.     float x, interval;
  260.     char s[cmdWordLen];
  261.     int oldWindow;
  262.     GrafPtr oldPort;
  263.     Rect tRect;
  264.  
  265.     GetPort( &oldPort );
  266.     oldWindow=currentWindow;
  267.  
  268.     SetPort( grPortPtr );
  269.     PenPat( black );
  270.     PenSize(1,1);
  271.     TextFont(monaco);
  272.     TextSize(9);
  273.     
  274.     SetRect( &tRect,  1, 1, (grXMin-1), (windowHeight-1) );
  275.     EraseRect(&tRect);
  276.     
  277.     SetRect( &tRect, 1, (grYMin+1), (windowWidth-1), (windowHeight-1) );
  278.     EraseRect(&tRect);
  279.  
  280.     MoveTo( grXMin,  grYMin ); /*x axis*/
  281.     LineTo(   grXMax, grYMin );
  282.     
  283.     MoveTo( grXMin, grYMin); /*y axis*/
  284.     LineTo(   grXMin, grYMax);
  285.         
  286.     interval = (graph.yMax-graph.yMin)/5.0;
  287.     for( i=0; i<=5; i++ ) {
  288.         x=graph.yMin + (float)(i)*interval;
  289.         GraphScale(graph.xMin,x,&k,&j);
  290.         MoveTo( grXMin, j );
  291.         LineTo( (grXMin+6), j );
  292.         RToS( x, s );
  293.         MoveTo( 5,  (j+4) );
  294.         DrawText( s, 0, strlen(s) );
  295.         }
  296.     
  297.     interval = (graph.xMax-graph.xMin)/5.0;
  298.     for (i=0; i<=5; i++ ) {
  299.         x=graph.xMin + (float)(i)*interval;
  300.         GraphScale(x,graph.yMin,&j,&k);
  301.         MoveTo( j, grYMin);
  302.         LineTo(   j, (grYMin-6) );
  303.         RToS( x, s );
  304.         width=0;
  305.         slen=strlen(s);
  306.         for( ii=0; ii<slen; ii++) {
  307.             width+=CharWidth(s[ii]);
  308.             }
  309.         MoveTo( (j-(width/2)),  (grYMin+15) );
  310.         DrawText( s, 0, slen );
  311.         }
  312.  
  313.     SetPort( theWindow[grWindow] );
  314.     InvalRect( &(theWindow[grWindow]->portRect) );
  315.     
  316.     SetPort( oldPort );
  317.     currentWindow=oldWindow;
  318.     whichWindow=theWindow[currentWindow];
  319. }